/**
 * 广寒宫
 * 网址:www.guanghangong.xyz
 */
package org.moon.framework.autoconfigure.oss;

import com.google.common.collect.Maps;
import org.moon.framework.autoconfigure.MoonConstants;
import org.moon.framework.autoconfigure.cache.CacheService;
import org.moon.framework.autoconfigure.config.SpringContextConfig;
import org.moon.framework.autoconfigure.exception.domain.MoonException;
import org.moon.framework.autoconfigure.oss.impl.*;
import org.moon.framework.autoconfigure.oss.props.OssProperties;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * 对象存储调用入口
 * @author moon
 */
@Configuration
public class OssTemplate {

    public final static String CACHE_OSS_CONFIG = "oss:config";

    public static Map<String, OssProvider> ossProviderMap = Maps.newHashMap();

    static {
        ossProviderMap.put(MoonConstants.OssTypeEnum.local.name(), new LocalOssProviderImpl());
        ossProviderMap.put(MoonConstants.OssTypeEnum.scp.name(), new ScpOssProviderImpl());
        ossProviderMap.put(MoonConstants.OssTypeEnum.aliyun.name(), new AliOssProviderImpl());
        ossProviderMap.put(MoonConstants.OssTypeEnum.minio.name(), new MinioOssProviderImpl());
        ossProviderMap.put(MoonConstants.OssTypeEnum.qiniu.name(), new QiniuOssProviderImpl());
    }

    /**
     * 获取对象存储操作对象
     */
    public static OssService getOssTemplate(){
        CacheService cacheService = SpringContextConfig.getBean(CacheService.class);
        OssProperties ossProperties = cacheService.get(CACHE_OSS_CONFIG, OssProperties.class);
        if(ossProperties==null){
            throw new MoonException(MoonConstants.OSS_CONFIG_NOT_EXISTS);
        }
        OssService ossService = ossProviderMap.get(ossProperties.getName()).builder(ossProperties);
        return ossService;
    }
}
